let address = require('../../common/city.js');
var flag=false;
const app = getApp();
Page({
  data:{
    name:"请填写您的姓名",
    tel:"请填写您的联系方式",
    door:"街道门牌信息",
    index:"0",
    addressMenuIsShow: false,
    value: [0, 0, 0],
    provinces: [],
    citys: [],
    areas: [],
    areaInfo:'',
    address_list:[],
    address_id:'',
  },
   onLoad: function(options) {
     
     wx.request({
       url: app.urlData.getURL+'/index.php/Mobile/Shopindex/edit_address',
       method: 'GET',
       data: {
         address_id:options.id
       },
       header: { 'Content-Type': 'application/json' },
       success: (res) => {
         console.log(res.data)
         this.setData({
           address_list: res.data.data,
           areaInfo: res.data.data.areaInfo,
           address_id: options.id
         });
       }
       
     })
   },
   // 点击所在地区弹出选择框
   selectDistrict: function () {
     var id = address.provinces[0].id
     this.setData({
       addressMenuIsShow: true,
       provinces: address.provinces,
       citys: address.citys[id],
       areas: address.areas[address.citys[id][0].id],
     })
   },
   // 点击地区选择取消按钮
   cityCancel: function () {
     this.setData({
       addressMenuIsShow: !this.data.addressMenuIsShow,
     })
   },
   // 点击地区选择确定按钮
   citySure: function (e) {
     var that = this
     var city = that.data.city
     var value = that.data.value
     // 将选择的城市信息显示到输入框
     var areaInfo = that.data.provinces[value[0]].name + ' ' + that.data.citys[value[1]].name + ' ' + that.data.areas[value[2]].name
     that.setData({
       areaInfo: areaInfo,
       addressMenuIsShow: !this.data.addressMenuIsShow,
     })
     console.log(areaInfo)
   },
   // 点击蒙版时取消组件的显示
   hideCitySelected: function (e) {
     console.log(e)
     this.startAddressAnimation(false)
   },
   // 处理省市县联动逻辑
   cityChange: function (e) {
     console.log(e)
     var value = e.detail.value
     var provinces = this.data.provinces
     var citys = this.data.citys
     var areas = this.data.areas
     var provinceNum = value[0]
     var cityNum = value[1]
     var countyNum = value[2]
     // 如果省份选择项和之前不一样，表示滑动了省份，此时市默认是省的第一组数据，
     if (this.data.value[0] != provinceNum) {
       var id = provinces[provinceNum].id
       this.setData({
         value: [provinceNum, 0, 0],
         citys: address.citys[id],
         areas: address.areas[address.citys[id][0].id],
       })
     } else if (this.data.value[1] != cityNum) {
       // 滑动选择了第二项数据，即市，此时区显示省市对应的第一组数据
       var id = citys[cityNum].id
       this.setData({
         value: [provinceNum, cityNum, 0],
         areas: address.areas[citys[cityNum].id],
       })
     } else {
       // 滑动选择了区
       this.setData({
         value: [provinceNum, cityNum, countyNum]
       })
     }
     console.log(this.data)
   },
  //点击删除
    delete:function(){
      var that = this;
      wx.showModal({
       title: '提示',
       content: '确认删除该地址信息吗？',
       success: (res)=> {
         if (res.confirm) {
           console.log('用户点击确定')
           wx.request({
             url: app.urlData.getURL+'/index.php/Mobile/Shopindex/del_address',
             method: 'GET',
             data: {
               user_id: wx.getStorageSync('user_id'),
               address_id: this.data.address_id
             },
             header: { 'Content-Type': 'application/json' },
             success: (res) => {
               console.log(res)
               wx.showToast({
                 title: res.data.msg,
                 icon: 'success',
                 duration: 2000
               })
              //  if (res.data.msg == '删除成功') {
              //    wx.showToast({
              //      title: '成功删除地址',
              //      icon: 'success',
              //      duration: 2000
              //    })
              //  }  
               setTimeout(function () {
                 wx.redirectTo({
                   url: '../address/address'
                 }); 
               }, 2000);
               
            
             }

           })
         
         } else if (res.cancel) {
           console.log('用户点击取消')
      }
    }
})

    },
//点击取消，返回上个页面
    cancel:function(){
      wx.navigateBack({
         delta: 1
      })
    },
    //点击保存
    formSubmit: function (e) {
      var warn = "";
      var that = this;
      var flag = false;
      if (e.detail.value.name == "") {
        warn = "请填写您的姓名！";
      } else if (e.detail.value.tel == "") {
        warn = "请填写您的手机号！";
      } else if (!(/^1(3|4|5|7|8)\d{9}$/.test(e.detail.value.tel))) {
        warn = "手机号格式不正确";
      } else if (that.data.areaInfo.length == '0') {
        warn = "请选择您的所在区域";
      } else if (e.detail.value.door == "") {
        warn = "请输入您的具体地址";
      } else {
        flag = true;
        wx.request({
          url: app.urlData.getURL +'/index.php/Mobile/Shopindex/do_address',
          data: {
            address_id: that.data.address_id,
            user_id: wx.getStorageSync('user_id'),
            tel:e.detail.value.tel,
            name:e.detail.value.name,
            areaInfo: that.data.areaInfo,
            door: e.detail.value.door,
            zipcode: 0
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          success: (res) => {

          }
        })
        
        wx.navigateBack({
          url: '../address/address?tel=' + e.detail.value.tel + "&name=" + e.detail.value.name + "&flag=" + flag + "&areaInfo=" + that.data.areaInfo + "&door=" + e.detail.value.door
          //？后面跟的是需要传递到下一个页面的参数

        });
        console.log("传过去的地址下标是多少？" + e.detail.value.addre)
      }
      if (flag == false) {
        wx.showModal({
          title: '提示',
          content: warn
        })
      }

    },
  
  })